package activator;

import java.util.Dictionary;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;

public class Activator implements BundleActivator {
	private static final String LIB_SERVICE_PID = "library.service.pid";
	ServiceReference configAdminServiceRef;

	public void start(BundleContext context) throws Exception {
		configAdminServiceRef = context
				.getServiceReference(ConfigurationAdmin.class.getName());
		if (configAdminServiceRef != null) {
			ConfigurationAdmin configAdmin = (ConfigurationAdmin) context
					.getService(configAdminServiceRef);
			if (configAdmin != null) {
				Configuration config = configAdmin
						.getConfiguration(LIB_SERVICE_PID);
				Dictionary libServiceConfigProps = config.getProperties();
				if (libServiceConfigProps != null) {
					config.update(libServiceConfigProps);
					System.out.println("libServiceConfigProps != null");
				} else {
					System.out.println("libServiceConfigProps == null");
				}
			} else {
				System.out.println("configAdmin == null");
			}
		} else {
			System.out.println("configAdminServiceRef == null");
		}
	}

	public void stop(BundleContext context) throws Exception {
		if (configAdminServiceRef != null) {
			context.ungetService(configAdminServiceRef);
		}

	}

}
